using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The StudentGuardianRelationshipTableDB class is responsible for interacting with the database to retrieve and store information
    /// about StudentGuardianRelationshipTable objects.
    /// </summary>
    public class StudentGuardianRelationshipTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of StudentGuardianRelationshipTable from the underlying datasource.
        /// </summary>
        /// <param name="guardianAccountKey">The unique GuardianAccountKey of the StudentGuardianRelationshipTable in the database.</param>
        /// <returns>An StudentGuardianRelationshipTable when the GuardianAccountKey was found in the database, or null otherwise.</returns>
        public static StudentGuardianRelationshipTable GetItem(int gsAutoID)
        {
            StudentGuardianRelationshipTable myStudentGuardianRelationshipTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentGuardianRelationshipTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@gsAutoID", gsAutoID);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myStudentGuardianRelationshipTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
            }
            return myStudentGuardianRelationshipTable;
        }


        /// <summary>
        /// Gets an instance of StudentGuardianRelationshipTable from the underlying datasource.
        /// </summary>
        /// <param name="StudentAccountKey">The unique StudentAccountKey of the StudentGuardianRelationshipTable in the database.</param>
        /// <returns>An StudentGuardianRelationshipTable when the GuardianAccountKey was found in the database, or null otherwise.</returns>
        public static StudentGuardianRelationshipTable GetItemByStudentAccountKey(int StudentAccountKey)
        {
            StudentGuardianRelationshipTable myStudentGuardianRelationshipTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentGuardianRelationshipTableSelectSingleItemByStudentAccountKey", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@StudentAccountKey", StudentAccountKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myStudentGuardianRelationshipTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myStudentGuardianRelationshipTable;
            }
        }

        /// <summary>
        /// Gets an instance of StudentGuardianRelationshipTable from the underlying datasource.
        /// </summary>
        /// <param name="StudentAccountKey">The unique StudentAccountKey of the StudentGuardianRelationshipTable in the database.</param>
        /// <returns>An StudentGuardianRelationshipTable when the GuardianAccountKey was found in the database, or null otherwise.</returns>
        public static StudentGuardianRelationshipTable GetItemByStudentAccountKey(int StudentAccountKey, bool OnlyMainGuardian)
        {
            StudentGuardianRelationshipTable myStudentGuardianRelationshipTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentGuardianRelationshipTableSelectSingleItemByStudentAccountKey", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@StudentAccountKey", StudentAccountKey);
                    myCommand.Parameters.AddWithValue("@OnlyMainGuardian", OnlyMainGuardian);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myStudentGuardianRelationshipTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myStudentGuardianRelationshipTable;
            }
        }

        /// <summary>
        /// Gets an instance of StudentGuardianRelationshipTable from the underlying datasource.
        /// </summary>
        /// <param name="StudentAccountKey">The unique StudentAccountKey of the StudentGuardianRelationshipTable in the database.</param>
        /// <returns>An StudentGuardianRelationshipTable when the GuardianAccountKey was found in the database, or null otherwise.</returns>
        public static StudentGuardianRelationshipTable GetItemByGuardianAccountKey(int GuardianAccountKey, bool OnlyMainGuardian)
        {
            StudentGuardianRelationshipTable myStudentGuardianRelationshipTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentGuardianRelationshipTableSelectSingleItemByGuardianAccountKey", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@GuardianAccountKey", GuardianAccountKey);
                    myCommand.Parameters.AddWithValue("@OnlyMainGuardian", OnlyMainGuardian);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myStudentGuardianRelationshipTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myStudentGuardianRelationshipTable;
            }
        }

        /// <summary>
        /// Gets an instance of StudentGuardianRelationshipTable from the underlying datasource.
        /// </summary>
        /// <param name="StudentAccountKey">The unique StudentAccountKey of the StudentGuardianRelationshipTable in the database.</param>
        /// <returns>An StudentGuardianRelationshipTable when the GuardianAccountKey was found in the database, or null otherwise.</returns>
        public static StudentGuardianRelationshipTable GetItemByGuardianAccountKey(int GuardianAccountKey)
        {
            StudentGuardianRelationshipTable myStudentGuardianRelationshipTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentGuardianRelationshipTableSelectSingleItemByGuardianAccountKey", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@GuardianAccountKey", GuardianAccountKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myStudentGuardianRelationshipTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myStudentGuardianRelationshipTable;
            }
        }


        /// <summary>
        /// Returns a list with StudentGuardianRelationshipTable objects.
        /// </summary>
        /// <returns>A generics List with the StudentGuardianRelationshipTable objects.</returns>
        public static StudentGuardianRelationshipTableList GetList()
        {
            StudentGuardianRelationshipTableList tempList = new StudentGuardianRelationshipTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentGuardianRelationshipTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Returns a list with StudentGuardianRelationshipTable objects.
        /// </summary>
        /// <returns>A generics List with the StudentGuardianRelationshipTable objects.</returns>
        public static StudentGuardianRelationshipTableList GetListByRelationshipTypeCode(short RelationshipTypeCode)
        {
            StudentGuardianRelationshipTableList tempList = new StudentGuardianRelationshipTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentGuardianRelationshipTableSelectListByRelationshipTypeCode", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@relationshipTypeCode", RelationshipTypeCode);
                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
                return tempList;
            }
        }

        /// <summary>
        /// Saves a StudentGuardianRelationshipTable in the database.
        /// </summary>
        /// <param name="myStudentGuardianRelationshipTable">The StudentGuardianRelationshipTable instance to save.</param>
        /// <returns>The new GuardianAccountKey if the StudentGuardianRelationshipTable is new in the database or the existing GuardianAccountKey when an item was updated.</returns>
        public static int Save(StudentGuardianRelationshipTable myStudentGuardianRelationshipTable)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentGuardianRelationshipTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myStudentGuardianRelationshipTable.GSAutoID == -1)
                    {
                        myCommand.Parameters.AddWithValue("@gsAutoID", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@gsAutoID", myStudentGuardianRelationshipTable.GSAutoID);
                    }

                    if (myStudentGuardianRelationshipTable.GuardianAccountKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@guardianAccountKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@guardianAccountKey", myStudentGuardianRelationshipTable.GuardianAccountKey);
                    }
                    if (myStudentGuardianRelationshipTable.StudentAccountKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@studentAccountKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@studentAccountKey", myStudentGuardianRelationshipTable.StudentAccountKey);
                    }
                    if (myStudentGuardianRelationshipTable.RelationshipTypeCode < 0)
                    {
                        myCommand.Parameters.AddWithValue("@relationshipTypeCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@relationshipTypeCode", myStudentGuardianRelationshipTable.RelationshipTypeCode);
                    }
                    if (string.IsNullOrEmpty(myStudentGuardianRelationshipTable.EmergencyContact))
                    {
                        myCommand.Parameters.AddWithValue("@emergencyContact", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@emergencyContact", myStudentGuardianRelationshipTable.EmergencyContact);
                    }
                    if (string.IsNullOrEmpty(myStudentGuardianRelationshipTable.Remark))
                    {
                        myCommand.Parameters.AddWithValue("@remark", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@remark", myStudentGuardianRelationshipTable.Remark);
                    }


                    myCommand.Parameters.AddWithValue("@IsMainGuardian", myStudentGuardianRelationshipTable.IsMainGuardian);
                    myCommand.Parameters.AddWithValue("@status", myStudentGuardianRelationshipTable.Status);

                    //if (myStudentGuardianRelationshipTable.CreatedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", myStudentGuardianRelationshipTable.CreatedDateTime);
                    //}
                    //if (myStudentGuardianRelationshipTable.ModifiedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", myStudentGuardianRelationshipTable.ModifiedDateTime);
                    //}

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a StudentGuardianRelationshipTable from the database.
        /// </summary>
        /// <param name="guardianAccountKey">The GuardianAccountKey of the StudentGuardianRelationshipTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(int guardianAccountKey)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentGuardianRelationshipTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@guardianAccountKey", guardianAccountKey);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the StudentGuardianRelationshipTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static StudentGuardianRelationshipTable FillDataRecord(IDataRecord myDataRecord)
        {
            StudentGuardianRelationshipTable myStudentGuardianRelationshipTable = new StudentGuardianRelationshipTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("GSAutoID")))
            {
                myStudentGuardianRelationshipTable.GSAutoID = myDataRecord.GetInt32(myDataRecord.GetOrdinal("GSAutoID"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StudentAccountKey")))
            {
                myStudentGuardianRelationshipTable.StudentAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("StudentAccountKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("GuardianAccountKey")))
            {
                myStudentGuardianRelationshipTable.GuardianAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("GuardianAccountKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("RelationshipTypeCode")))
            {
                myStudentGuardianRelationshipTable.RelationshipTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("RelationshipTypeCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EmergencyContact")))
            {
                myStudentGuardianRelationshipTable.EmergencyContact = myDataRecord.GetString(myDataRecord.GetOrdinal("EmergencyContact"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remark")))
            {
                myStudentGuardianRelationshipTable.Remark = myDataRecord.GetString(myDataRecord.GetOrdinal("Remark"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("IsMainGuardian")))
            {
                myStudentGuardianRelationshipTable.IsMainGuardian = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("IsMainGuardian"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myStudentGuardianRelationshipTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myStudentGuardianRelationshipTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myStudentGuardianRelationshipTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myStudentGuardianRelationshipTable;
        }
    }

}
